package com.minyifang.wcspf.work.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.work.ui.goods.model.ConfirmOrderBean;
import com.minyifang.wcspf.work.ui.user.model.AddressDialogBean;

import java.util.List;

/**
 * Created by LuoYaWei on 2018/8/01.
 */
public class OrderTimeChooseDialog extends Dialog {

    public OrderTimeChooseDialog(@NonNull Context context) {
        super(context);
    }

    public OrderTimeChooseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置dialog 窗体的宽度
        if (getWindow() != null) {
            getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
            getWindow().getAttributes().gravity = Gravity.CENTER_VERTICAL;
        }
    }

    public static class Builder {
        private Context mContext;

        private List<ConfirmOrderBean.TimeBean> mList;
        private OrderTimeChooseAdapter mAdapter;

        private OnItemClickListener mListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setList(List<ConfirmOrderBean.TimeBean> list) {
            mList = list;
            return this;
        }

        public Builder setOnItemClickListener(OnItemClickListener listener) {
            mListener = listener;
            return this;
        }

        public OrderTimeChooseDialog build() {

            final OrderTimeChooseDialog dialog = new OrderTimeChooseDialog(mContext, R.style.CustomDialog);

            View layout = View.inflate(mContext, R.layout.layout_add_address_dialog, null);

            RecyclerView recyclerView = layout.findViewById(R.id.rv_list);
            mAdapter = new OrderTimeChooseAdapter(R.layout.item_order_time_choose_dialog, mList);
            recyclerView.setAdapter(mAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));

            mAdapter.setOnItemClickListener((adapter, view, position) -> {
                if (mListener != null) {
                    mListener.onItemClick(dialog, position);
                }
            });

            //将布局添加到 dialog 中
            dialog.setContentView(layout);
            return dialog;
        }

        public class OrderTimeChooseAdapter extends BaseQuickAdapter<ConfirmOrderBean.TimeBean, BaseViewHolder> {

            public OrderTimeChooseAdapter(int layoutResId, @Nullable List<ConfirmOrderBean.TimeBean> data) {
                super(layoutResId, data);
            }

            @Override
            protected void convert(BaseViewHolder helper, ConfirmOrderBean.TimeBean item) {
                helper.setText(R.id.item_time, item.getName());
                helper.setVisible(R.id.item_isFull, item.getType().equals("1"));
            }
        }

        public interface OnItemClickListener {
            void onItemClick(OrderTimeChooseDialog dialog, int position);
        }
    }
}
